package com.api.hg;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.api.sw.duban.util.LogTool;
import weaver.conn.RecordSet;
import weaver.general.Util;

import java.util.Arrays;

public class BaseUtle {
    private final static LogTool log = new LogTool("/log/assetlog/luolf/BaseUtle",false);
    public static int sysadmin=42;//超级管理员  海关  42  本地 29
    public static int zssysadmin=45;//中山海关   海关  45  本地 26
    public static int subsysadmin=43;//分部       海关  43  本地 28
    public static int bmsysadmin=44;//科室        海关  44  本地 27
    public static String subtable="uf_bmglysq";//分部表
    public static String bmbtable="uf_ksglysq";//部门表
    public static String submodeid="167";//分部管理员模块ID   海关  104  本地 23  生产 167
    public static String depmodeid="165";//部门管理员模块ID  海关  105 本地 24   生产 165
    public static int zshgsub=5;//中山海关分部ID  海关  16  本地 14  生产 5

    public static String subdefined="swzcgly";//分部自定义字段    海关 swzcgly  本地fbzcgly
    public static String depefined="swzcgly";//部门自定义字段   海关swzcgly    本地bmzcgly
    public static String defindsub="1";//默认顶级分部 海关 1  本地 0

    public static String txwf="165";//资产提醒流程
    public static double txbz=30;//帐时不符率标准
    public static String bflCustomid="199";//不符率查询id

    public static int swmodeid=102;//实物模型ID
    public static int gbmodeid=101;//关保模型ID
    /*
        获取更节点的分部
     */
    public String getRootSubCompay(String substr){
        String root="";
        substr= Util.null2String(substr).trim();
        if(BaseUtle.defindsub.equals(substr)){
            return BaseUtle.defindsub;
        }
        if(substr.endsWith(",")){
            substr=substr.substring(0,substr.length()-1);
        }
        if(substr.startsWith(",")){
            substr=substr.substring(1);
        }
        if("".equals(substr)){
            return "";
        }
        RecordSet re=new RecordSet();
        String sql="with Department as " +
                " (select *,0 as Level from HrmSubCompany where id in ("+substr+") and isnull(canceled,0)=0" +
                " union all select d.*,Department.Level + 1 from Department,HrmSubCompany d where Department.id=d.supsubcomid and isnull(d.canceled,0)=0) " +
                " select id from (select id,SubCompanyname,Level from Department where id in ("+substr+")  ) a group by id having COUNT(1)=1";
        log.writeLog("getRootSubCompay--->"+sql);
        re.execute(sql);
        while(re.next()){
            String id=Util.null2String(re.getString("id")).trim() ;
            root=root+id+",";
        }
        if(root.endsWith(",")){
            root=root.substring(0,root.length()-1);
        }
        log.writeLog("getRootSubCompayroot--->"+root);
        return root;
    }
    /*
        获取更节点的分部下面的部门
     */
    public String getRootDep(String substr){
        String root="";
        substr= Util.null2String(substr).trim();
        if("0".equals(substr)){
            return "0";
        }
        if(substr.endsWith(",")){
            substr=substr.substring(0,substr.length()-1);
        }
        if(substr.startsWith(",")){
            substr=substr.substring(1);
        }
        if("".equals(substr)){
            return "";
        }
        RecordSet re=new RecordSet();
        String sql="select * from hrmdepartment where isnull(supdepid,0)=0 and isnull(canceled,0)=0 and  subcompanyid1 in ("+substr+")";
        log.writeLog("getRootDep--->"+sql);
        re.execute(sql);
        while(re.next()){
            String id=Util.null2String(re.getString("id")).trim() ;
            root=root+id+",";
        }
        if(root.endsWith(",")){
            root=root.substring(0,root.length()-1);
        }
        log.writeLog("getRootSubCompayroot--->"+root);
        return root;
    }
    /*
        获取部门树
     */
    public JSONArray getDepTree(String rootSub){
        JSONArray Rootjsons=new JSONArray();
        String rootSubdemo=rootSub;
        rootSub= Util.null2String(rootSub).trim();
        rootSub=getRootDep(rootSub);
        if("".equals(rootSub)){
            return Rootjsons;
        }

        String[] rootSubs=rootSub.split(",");
        for(String str:rootSubs){
            str= Util.null2String(str).trim();
            if("".equals(str)){
                continue;
            }
            //获取根节点的 json  并且设置为不可选中
            JSONObject rootjson= getDepJSON(str);
            if(rootjson!=null&&rootjson.size()>0){
                JSONArray jsons=new JSONArray();
                rootjson.put("children",jsons);
                setTreeDep(jsons,null,str,0);
                Rootjsons.add(rootjson);
            }

        }
        //封装分部的  json
        JSONArray subjsonroots=new JSONArray();
        JSONObject subjsonroot=getSubJSON(rootSubdemo);
        subjsonroot.put("disabled",true);
        if(Rootjsons!=null&&Rootjsons.size()>0){
            subjsonroot.put("children",Rootjsons);
        }
        subjsonroots.add(subjsonroot);
        //json.put("disabled",true);
        return subjsonroots;
    }
    /*
        获取分部树
     */
    public JSONArray getSubTree(String rootSub){
        JSONArray Rootjsons=new JSONArray();
        rootSub= Util.null2String(rootSub).trim();
        rootSub=getRootSubCompay(rootSub);
        if("".equals(rootSub)){
            return Rootjsons;
        }

        String[] rootSubs=rootSub.split(",");
        for(String str:rootSubs){
            str= Util.null2String(str).trim();
            if("".equals(str)){
                continue;
            }
            //获取根节点的 json  并且设置为不可选中
            JSONObject rootjson= getSubJSON(str);
            if(rootjson!=null&&rootjson.size()>0){
                JSONArray jsons=new JSONArray();
                rootjson.put("children",jsons);
                setTree(jsons,null,str,0);
                Rootjsons.add(rootjson);
            }

        }
        return Rootjsons;
    }
    /*
       获取分部树  用于超级管理员分配 科室
    */
    public JSONArray getSubTreeDispaly(String rootSub){
        JSONArray Rootjsons=new JSONArray();
        rootSub= Util.null2String(rootSub).trim();
        rootSub=getRootSubCompay(rootSub);
        if("".equals(rootSub)){
            return Rootjsons;
        }

        String[] rootSubs=rootSub.split(",");
        for(String str:rootSubs){
            str= Util.null2String(str).trim();
            if("".equals(str)){
                continue;
            }
            //获取根节点的 json  并且设置为不可选中
            JSONObject rootjson= getSubJSON(str);
            if(rootjson!=null&&rootjson.size()>0){
                JSONArray jsons=new JSONArray();
                rootjson.put("children",jsons);
                setTreeDisplay(jsons,null,str,0);
                Rootjsons.add(rootjson);
            }

        }
        return Rootjsons;
    }
    /*
            遍历树节点
            label: '节点一',
            value: '0-0',
            key: '0-0',
            children
         */
    public void setTreeDep(JSONArray jsons, JSONObject perent,String subid, int len){
        RecordSet re=new RecordSet();
        String sql=" select id,departmentname from  hrmdepartment where isnull(canceled,0)=0 and isnull(supdepid,0)='"+subid+"'  order by showorder";
        log.writeLog(sql);
        re.execute(sql);
        JSONObject jsonDemo=null;
        while(re.next()){
            String id=Util.null2String(re.getString("id")).trim() ;
            String SubCompanyname=Util.null2String(re.getString("departmentname")).trim() ;
            jsonDemo=new JSONObject();
            jsonDemo.put("label",SubCompanyname);
            jsonDemo.put("value","dep_"+id);
            jsonDemo.put("key","dep_"+id);
            //判断是否是 root
            if(perent==null||perent.size()==0){
                jsons.add(jsonDemo);
                JSONArray childrendemo=new JSONArray();
                //jsonDemo.put("children",childrendemo);
                setTreeDep( jsons,  jsonDemo,id, 0);
            }else{//不是跟
                JSONArray children=perent.getJSONArray("children");
                if(children==null){

                    children=new JSONArray();
                    perent.put("children",children);
                }
                children.add(jsonDemo);
                //JSONArray childrendemo=new JSONArray();
                //jsonDemo.put("children",childrendemo);
                //children.add(jsonDemo);
                setTreeDep( jsons,  jsonDemo,id, 0);
            }
        }
    }
    /*
        遍历树节点
        label: '节点一',
        value: '0-0',
        key: '0-0',
        children
     */
    public void setTree(JSONArray jsons, JSONObject perent,String subid, int len){
            RecordSet re=new RecordSet();
            String sql=" select id,SubCompanyname from  HrmSubCompany where isnull(canceled,0)=0 and isnull(supsubcomid,0)='"+subid+"' order by showorder ";
            log.writeLog(sql);
            re.execute(sql);
            JSONObject jsonDemo=null;
            while(re.next()){
                String id=Util.null2String(re.getString("id")).trim() ;
                String SubCompanyname=Util.null2String(re.getString("SubCompanyname")).trim() ;
                jsonDemo=new JSONObject();
                jsonDemo.put("label",SubCompanyname);
                jsonDemo.put("value","sub_"+id);
                jsonDemo.put("key","sub_"+id);
                //判断是否是 root
                if(perent==null||perent.size()==0){
                    jsons.add(jsonDemo);
                    JSONArray childrendemo=new JSONArray();
                    //jsonDemo.put("children",childrendemo);
                    setTree( jsons,  jsonDemo,id, 0);
                }else{//不是跟
                    JSONArray children=perent.getJSONArray("children");
                    if(children==null){

                        children=new JSONArray();
                        perent.put("children",children);
                    }
                    children.add(jsonDemo);
                    //JSONArray childrendemo=new JSONArray();
                    //jsonDemo.put("children",childrendemo);
                    //children.add(jsonDemo);
                    setTree( jsons,  jsonDemo,id, 0);
                }
            }
    }
    /*
        用来提供 给超级管理员的
       遍历树节点
       label: '节点一',
       value: '0-0',
       key: '0-0',
       children
    */
    public void setTreeDisplay(JSONArray jsons, JSONObject perent,String subid, int len){
        RecordSet re=new RecordSet();
        String sql=" select id,SubCompanyname from  HrmSubCompany where isnull(canceled,0)=0 and isnull(supsubcomid,0)='"+subid+"' order by showorder ";
        log.writeLog(sql);
        re.execute(sql);
        JSONObject jsonDemo=null;
        while(re.next()){
            String id=Util.null2String(re.getString("id")).trim() ;
            String SubCompanyname=Util.null2String(re.getString("SubCompanyname")).trim() ;
            jsonDemo=new JSONObject();
            jsonDemo.put("label",SubCompanyname);
            jsonDemo.put("value","sub_"+id);
            jsonDemo.put("key","sub_"+id);
            jsonDemo.put("disabled",true);
            jsonDemo.put("children",getDepTreeDisplay(id));//获取分部下面的部门的部门
            //判断是否是 root
            if(perent==null||perent.size()==0){
                jsons.add(jsonDemo);
                JSONArray childrendemo=new JSONArray();
                //jsonDemo.put("children",childrendemo);
                setTreeDisplay( jsons,  jsonDemo,id, 0);
            }else{//不是跟
                JSONArray children=perent.getJSONArray("children");
                if(children==null){

                    children=new JSONArray();
                    perent.put("children",children);
                }
                children.add(jsonDemo);
                //JSONArray childrendemo=new JSONArray();
                //jsonDemo.put("children",childrendemo);
                //children.add(jsonDemo);
                setTreeDisplay( jsons,  jsonDemo,id, 0);
            }
        }
    }
    /*
       获取部门树
    */
    public JSONArray getDepTreeDisplay(String rootSub){
        JSONArray Rootjsons=new JSONArray();
        String rootSubdemo=rootSub;
        rootSub= Util.null2String(rootSub).trim();
        rootSub=getRootDep(rootSub);
        if("".equals(rootSub)){
            return Rootjsons;
        }

        String[] rootSubs=rootSub.split(",");
        for(String str:rootSubs){
            str= Util.null2String(str).trim();
            if("".equals(str)){
                continue;
            }
            //获取根节点的 json  并且设置为不可选中
            JSONObject rootjson= getDepJSON(str);
            if(rootjson!=null&&rootjson.size()>0){
                JSONArray jsons=new JSONArray();
                rootjson.put("children",jsons);
                setTreeDep(jsons,null,str,0);
                Rootjsons.add(rootjson);
            }

        }
        //封装分部的  json
       // JSONArray subjsonroots=new JSONArray();
       // JSONObject subjsonroot=getSubJSON(rootSubdemo);
       // subjsonroot.put("disabled",true);
       // if(Rootjsons!=null&&Rootjsons.size()>0){
       //     subjsonroot.put("children",Rootjsons);
       // }
       // subjsonroots.add(subjsonroot);
        //json.put("disabled",true);
       // return subjsonroots;
        return Rootjsons;
    }
    /*
        获取分部的json
     */
    public JSONObject getSubJSON(String subid){
        JSONObject json=new JSONObject();
        RecordSet re=new RecordSet();
        if(!BaseUtle.defindsub.equals(subid)){
            String sql=" select id,SubCompanyname from  HrmSubCompany   where id="+subid;
            re.execute(sql);
            while(re.next()){
                String id=Util.null2String(re.getString("id"));
                String SubCompanyname=Util.null2String(re.getString("SubCompanyname"));
                json.put("label",SubCompanyname);
                json.put("value","sub_"+id);
                json.put("key","sub_"+id);
                json.put("disabled",true);
            }
        }else{
            json.put("label","拱北海关");
            json.put("value","sub_0");
            json.put("key","sub_0");
            json.put("disabled",true);
        }

        return json;
    }
    /*
       获取分部的json
    */
    public JSONObject getDepJSON(String depid){
        JSONObject json=new JSONObject();
        RecordSet re=new RecordSet();

            String sql=" select id,departmentname from  hrmdepartment  where id="+depid;
            re.execute(sql);
            while(re.next()){
                String id=Util.null2String(re.getString("id"));
                String departmentname=Util.null2String(re.getString("departmentname"));
                json.put("label",departmentname);
                json.put("value","dep_"+id);
                json.put("key","dep_"+id);
                //json.put("disabled",true);
            }


        return json;
    }
    /*
        获取  分部管理员的数据
     */
    public void getSubModelData(String billid,JSONObject data){
            if(data==null){
                data=new JSONObject();
            }
            JSONArray hrms=new JSONArray();
            //JSONObject data=new JSONObject();
            String[] tree=new String[0];
            RecordSet re=new RecordSet();
            String sql="select * from uf_bmglysq where id ="+billid;
            re.execute(sql);
            if(re.next()){
                String bmmc=Util.null2String(re.getString("bmmc"));
                String zcgly=Util.null2String(re.getString("zcgly"));
                tree = Arrays.copyOf(tree, (tree.length+1));
                tree[tree.length-1] = "sub_"+bmmc;
                //获取人员信息
                hrms=getHrmJSON(zcgly);
            }
            data.put("selectsubtreeData",tree);
            data.put("selectry",hrms);

    }
    /*
       获取  部门管理员的数据
    */
    public void getDepModelData(String billid,JSONObject data){
        if(data==null){
            data=new JSONObject();
        }
        JSONArray hrms=new JSONArray();
        //JSONObject data=new JSONObject();
        String[] tree=new String[0];
        RecordSet re=new RecordSet();
        String sql="select * from "+BaseUtle.bmbtable+" where id ="+billid;
        re.execute(sql);
        if(re.next()){
            String bmmc=Util.null2String(re.getString("bmmc"));
            String zcgly=Util.null2String(re.getString("zcgly"));
            tree = Arrays.copyOf(tree, (tree.length+1));
            tree[tree.length-1] = "dep_"+bmmc;
            //获取人员信息
            hrms=getHrmJSON(zcgly);
        }
        data.put("selectsubtreeData",tree);
        data.put("selectry",hrms);

    }
    /*
        获取多人资源的姓名
     */
    public JSONArray getHrmJSON(String hrmid){
        JSONArray hrms=new JSONArray();
        JSONObject json=null;
        hrmid= Util.null2String(hrmid).trim();
        if(hrmid.endsWith(",")){
            hrmid=hrmid.substring(0,hrmid.length()-1);
        }
        if("".equals(hrmid)){
            return hrms;
        }

        RecordSet re=new RecordSet();
        String sql="select * from hrmresource  where id in("+hrmid+")";
        re.execute(sql);
        while(re.next()){
            json=new JSONObject();
            String id=Util.null2String(re.getString("id"));
            String lastname=Util.null2String(re.getString("lastname"));
            json.put("id",id);
            json.put("name",lastname);
            hrms.add(json);
        }
        return hrms;
    }
    //暂时不需要
    public String getRootSub(){
        RecordSet re=new RecordSet();
        String sql=" select id,SubCompanyname from  HrmSubCompany  where isnull(canceled,0)=0 and isnull(supsubcomid,0)=0";
        re.execute(sql);
        while(re.next()){

        }
        return "";
    }
    /*
        判断角色是否包含人员
     */
    public static boolean ckeckRole(String userid,String role){
        RecordSet re=new RecordSet();
        String sql=" select 1 from hrmrolemembers where roleid in ("+role+") and resourceid in ("+userid+")" +
                   " union all " +
                   " select 1 from hrmrolemembers a,HrmDepartment c,hrmresource d  where roleid in ("+role+") and a.resourcetype=3 and a.resourceid=c.id " +
                   " and d.departmentid=c.id and d.id in ("+userid+")";
        log.writeLog("ckeckRole--->"+sql);
        re.execute(sql);
        if(re.next()){
            return true;
        }else{
            return false;
        }
    }
    /*
        获取分部的以及部门的 字长管理员
     */
    public String getSubidByMax(String userid,String lx){
            String root="";
            lx= Util.null2String(lx).trim();
            RecordSet re=new RecordSet();
            String sql="";
            if("0".equals(lx)){//分部
                sql=" select distinct subcomid from  HrmSubcompanyDefined where ','+CAST( "+subdefined+" as varchar(1000) ) +',' like '%,"+userid+",%' ";
            }
            if("1".equals(lx)){//部门
                sql=" select distinct deptid subcomid from HrmDepartmentDefined where ','+CAST( "+depefined+" as varchar(1000) ) +',' like '%,"+userid+"%,' ";
            }
            re.execute(sql);
            while(re.next()){
                String id=Util.null2String(re.getString("subcomid"));
                root=root+id+",";
            }
            if(root.endsWith(",")){
                root=root.substring(0,root.length()-1);
            }
            return root;

    }
    /*
        判断人员是和否 存在 矩阵
     */
    public static boolean checkSubMaxibyHrm(String userid,String sudid){
        userid= Util.null2String(userid).trim();
        sudid= Util.null2String(sudid).trim();
        if("".equals(userid)){
            return false;
        }
        RecordSet re=new RecordSet();
        String sql="";
        if("".equals(sudid)){
            sql="select 1 from HrmSubcompanyDefined where ','+CAST( "+subdefined+" as varchar(1000) ) +',' like '%"+userid+"%'   ";
        }else{
            sql="select 1 from HrmSubcompanyDefined where ','+CAST( "+subdefined+" as varchar(1000) ) +',' like '%"+userid+"%' and subcomid not in ("+sudid+")  ";
        }
        re.execute(sql);
        if(re.next()){
            return true;
        }else{
            return false;
        }
    }
    /*
        判断人员是和否 存在 矩阵
     */
    public static boolean checkDepMaxibyHrm(String userid,String depid){
        userid= Util.null2String(userid).trim();
        depid= Util.null2String(depid).trim();
        if("".equals(userid)){
            return false;
        }
        RecordSet re=new RecordSet();
        String sql="";
        if("".equals(depid)){
            sql="select 1 from HrmDepartmentDefined where ','+CAST( "+depefined+" as varchar(1000) ) +',' like '%"+userid+"%'   ";
        }else{
            sql="select 1 from HrmDepartmentDefined where ','+CAST( "+depefined+" as varchar(1000) ) +',' like '%"+userid+"%' and deptid not in ("+depid+")  ";
        }
        re.execute(sql);
        if(re.next()){
            return true;
        }else{
            return false;
        }
    }
    /*
        获取矩阵人员
     */
    public static String getHrmidByMax(String id,String lx){
        String root="";
        lx= Util.null2String(lx).trim();
        RecordSet re=new RecordSet();
        String sql="";
        if("0".equals(lx)){//分部
            sql=" select "+subdefined+" from  HrmSubcompanyDefined where   subcomid="+id;
        }
        if("1".equals(lx)){//部门
            sql=" select "+depefined+" from HrmDepartmentDefined where  deptid="+id;
        }
        re.execute(sql);
        while(re.next()){
            if("0".equals(lx)){
                root=Util.null2String(re.getString(subdefined));
            }
            if("1".equals(lx)){
                root=Util.null2String(re.getString(depefined));
            }
        }
        if(root.endsWith(",")){
            root=root.substring(0,root.length()-1);
        }
        return root;

    }
    /*

     判断角色是否包含人员

  */
    public static String  getRoleHrm (String role){
        RecordSet re=new RecordSet();
        String resourceid="";
        String sql=" select resourceid from hrmrolemembers where resourcetype=1 and roleid in ("+role+") " +
                " union all " +
                " select d.id resourceid from hrmrolemembers a,HrmDepartment c,hrmresource d  where roleid in ("+role+") and a.resourcetype=3 and a.resourceid=c.id " +
                " and d.departmentid=c.id ";
        log.writeLog("ckeckRole--->"+sql);
        re.execute(sql);
        if(re.next()){
            String str=Util.null2String(re.getString("resourceid"));
            if(!"".equals(str)){
                resourceid=resourceid+str+",";
            }
        }
        return resourceid;
    }

    /**
     * 获取部门对应的分部id
     * @param depid
     * @return
     */
    public  static  String getSubidBydepid(String depid){
        RecordSet re=new RecordSet();
        String sql = "select subcompanyid1 from HrmDepartment where id="+depid;
        re.execute(sql);
        if(re.next()){
            return  re.getString("subcompanyid1");
        }
        return "";
    }
}
